package com.budwk.app.waterquality.component;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sys.models.Sys_unit;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUnitService;
import com.budwk.app.utils.NutzIocUtils;
import com.budwk.app.waterquality.config.AppException;
import com.budwk.app.waterquality.enums.WaterQEnum;
import com.budwk.app.waterquality.models.WaterQualityIndicator;
import com.budwk.app.waterquality.vo.ParamProblemVo;
import com.budwk.app.waterquality.vo.WaterQualityCommonVo;
import com.budwk.app.waterquality.vo.WaterQualityVo;
import com.budwk.starter.common.page.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.lang.Strings;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class WaterQualityComponent {

    public static void checkAbnormalMarkingParam(ParamProblemVo problemVo) {
        if (StringUtils.isEmpty(problemVo.getId())) {
            throw new AppException("数据不存在！");
        }
        if (StringUtils.isEmpty(problemVo.getField())) {
            throw new AppException("标记字段不能为空！");
        }
    }

    public static void initAddIndicator(WaterQualityIndicator indicator) {

        WaterQualityVo w = JSONObject.parseObject(indicator.getWaterQualityData(), WaterQualityVo.class);
        List<ParamProblemVo> paramProblemVos = w.getWaterParam();
        for (ParamProblemVo p : paramProblemVos) {
            Optional<WaterQEnum> optional = Arrays.stream(WaterQEnum.values()).filter(e -> e.getField().equals(p.getField())).findFirst();
            if (optional.isPresent()) {
                WaterQEnum e = optional.get();
                p.setFieldUnit(e.getFieldUnit()).setName(e.getName()).setIsException("否");
            }
        }
        w.setWaterParam(paramProblemVos);
        Sys_user user = CommonComponent.getCurrentUser();
        indicator.setCreateId(user.getId())
                .setCreateDate(LocalDateTime.now())
                .setUpdateId(user.getId())
                .setUpdateDate(LocalDateTime.now())
                .setUnitId(user.getUnitId());
        indicator.setWaterQualityData(JSONObject.toJSONString(w));

        SysUnitService unitService = NutzIocUtils.getBean(SysUnitService.class);
        if (StringUtils.isNotEmpty(user.getUnitId())) {
            Sys_unit unit = unitService.fetch(user.getUnitId());
            if (unit != null) {
                indicator.setUnitName(unit.getName());
            }
        }
    }

    public static Cnd bulidQueryCondition(WaterQualityCommonVo reqVo) {
        Cnd cnd = Cnd.NEW();
        SqlExpressionGroup where = cnd.where();
        if (Strings.isNotBlank(reqVo.getPageOrderBy())) {
            cnd.orderBy(reqVo.getPageOrderName(), PageUtil.getOrder(reqVo.getPageOrderBy()));
        }
        if (StringUtils.isNotEmpty(reqVo.getUnitName())) {
            where.andLike(WaterQualityIndicator::getUnitName, reqVo.getUnitName());
        }
        if (StringUtils.isNotEmpty(reqVo.getStnm())) {
            where.andEquals("water_quality_data->>'STNM'", reqVo.getStnm());
        }
        if (StringUtils.isNotEmpty(reqVo.getSpt())) {
            String[] split = reqVo.getSpt().split(",");
            String start = split[0] + " 00:00:00";
            String end = split[1] + " 23:59:59";
            where.andBetween("water_quality_data->>'SPT'", start, end);
        }
        return cnd;
    }
}
